// yb_mingpian/pages/miaosha/seckillList/index.js
var t = getApp(),
  a = t.requirejs("core");
Page({
  data: {
    currentTab: 1,
    page: 0,
    pageList: [],
    countDownList: [],
    loading: true,
    countDownList2: [],
    timeList: [],
    interval1: 0,
    interval2: 0
  },

  onLoad: function (options) {
    this.setCurrentData()
  },

  onShow: function () {

  },

  // 请求列表数据
  setCurrentData: function () {
    var t = this;
    var currentTab = t.data.currentTab;
    t.setData({
      loading: true
    }),
    a.get("Miaosha/msGoodsList", {
        page: t.data.page,
        ms_type: t.data.currentTab,
    }, function (e) {
        if(e.code==0){
            var time_list = [];
            if (e.info.length > 0) {
              e.info.forEach(function(v,k){
                  time_list[k] = v.etime;
              })
            }

            t.setData({
              loading: false,
              show: true,
              timeList: time_list
            });
            console.log("mslist",t.data.timeList)

            // 页面数据内容
            e.info.length > 0 && t.setData({
              page: t.data.page + 1,
              pageList: t.data.pageList.concat(e.info)
            });
            e.info.length < 10 && t.setData({
              loaded: true
            })

            if (time_list.length > 0) {
              if (currentTab == 1) {
                // 先清除倒计时
                clearInterval(t.data.interval1);
                clearInterval(t.data.interval2);
                // 执行倒计时
                var timeInterval1 = setInterval(function () {
                  a.newCountDown(t.data.timeList, t)
                }, 1000);
                t.setData({
                  interval1: timeInterval1
                });
              } else {
                // 先清除倒计时
                clearInterval(t.data.interval1);
                clearInterval(t.data.interval2);
                // 执行倒计时
                var timeInterval2 = setInterval(function () {
                  a.newCountDown(t.data.timeList, t)
                }, 1000);
                t.setData({
                  interval2: timeInterval2
                });
              }
            }
          }else{
            a.alert(e.msg)
          }
        }, true)
  },

  // 点击tab切换 
  changeList: function (e) {
    if (this.data.currentTab == e.currentTarget.dataset.list) return;
    this.setData({
      currentTab: e.currentTarget.dataset.list
    })
    this.setData({
      pageList: [],
      page: 1,
      loaded: false,
    })
    this.setCurrentData();
  },

  /**
   * 下拉刷新
   */
  onPullDownRefresh: function () {
    this.setData({
      pageList: [],
      page: 1,
      loaded: false
    })
    this.setCurrentData()
    wx.stopPullDownRefresh();
  },
  /**
  *上拉加载
  */
  onReachBottom: function () {
    if (!this.data.loaded) {
      console.log('加载更多')
      this.setCurrentData()
    }
  },
  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {
  //
  // }
})